<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use app\api\controller\Index;
use app\index\controller\PaymentController;
class CityController extends BaseController
{	
	protected function _initialize() {
	   $type = get_brower_type();
	   $this->assign('browder_type',$type);
	    $user_info = db('member')->alias("a")->join('member b',"a.pid1=b.id","left")
	   ->where('a.id',session('user_info')['id'])
	   ->field("a.*,b.nick_name as pname,b.real_name as prname")
	   ->find();
	   $this->assign("pname",$user_info['prname']?$user_info['prname']:$user_info['pname']);
		if($user_info['is_city']==1){
			$this->assign("jsname",'美丽导师合伙人');
		}elseif($user_info['is_pt']==1){
			$this->assign("jsname",'美丽事业合伙人');
		}elseif($user_info['is_rz']==1&&$user_info['is_qy']){
			$this->assign("jsname",'形象美学代言人');
		}elseif($user_info['is_rz']==1){
			$this->assign("jsname",'轻创平台创客');
		}else{
			$this->assign("jsname",'注册创客');
		}
		session("user_info",$user_info);
       parent::_initialize();
    }
	//审核订货
	public function dh_sh(){
		if($_POST){
			$res = db('dinghuo')->where("id",input("id"))->update(['status'=>input("status")]);
			if($res){
				return json(['code'=>200,'msg'=>'操作成功']);
			}
			return json(['code'=>400,'msg'=>'操作失败']);
		}	
	}
	//合伙人业绩管理
	public function dinghuo_list(){
		$w['city_id'] = session('user_info')['id'];
		if(input('name')){
			$w['user_id|name|mobile'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('dinghuo')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}	
		return $this->fetch();
	}
	public function lh_csr(){
		return $this->fetch();
	}
	//会员管理与查询
	public function huiyuan(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		if($user_info["is_city"]==1){
			$w['a.city_id']=$user_id;
		}else{
			$w['a.pt_id']=$user_id;
		}
		
		//$w['a.is_rz']=1;

		if($_POST){
			if(input('name')){
			$w['a.id|a.mobile|a.real_name'] = ['like','%'.input('name').'%'];
			}
			$list = db('member')->alias('a')
			->join("member b","a.pid1=b.id")->where($w)->field("a.*,b.nick_name as b")->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}	
		$data['count'] = db('member')->alias('a')->where($w)->count();
		$data['month'] = db('member')->alias('a')->where($w)->wheretime("a.add_time","month")->count();
		$data['today'] = db('member')->alias('a')->where($w)->wheretime("a.add_time","today")->count();
		$this->assign('data',$data);
		return $this->fetch();
	}
	public function update_mem(){
			$param=input('post.');
			if($param['password']){
				$param['password']=md5($param['password']);
			}
    		$rest=db('member')->where('id',$param['id'])->update($param);
    		if($rest){
    			return json(['code'=>200,'msg'=>'保存成功']);
			}else{
				return json(['code'=>200,'msg'=>'保存失败']);
			}
	}
	//会员设置
	public function set(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		$w['city_id']=$user_id;
		$w['is_rz']=1;
		$w['is_pt']=['neq',1];
		if($_POST){
			if(input('name')){
			$w['id|mobile|real_name'] = ['like','%'.input('name').'%'];
			}
			$list = db('member')->where($w)->order("add_time desc")->page(1,3)->select();
			return json(['list'=>$list]);
		}	
		return $this->fetch();
	}
	//运营中心
    public function index()
    {  	
		$info_user = session('user_info');
//		if(!$info_user['logo']){
//			parent::up_user(session("openid"));
//		}

		$myfriends=db("member")->where("pid1",$info_user['id'])->count();
		$this->assign("counts",$myfriends);
		
		$ptcount=db("member")->where("old_pt",$info_user['id'])->count();
		$this->assign("ptcount",$ptcount);
    	$this->assign('info_user',$info_user);
		
		$tjname = db("member")->where("id",$info_user['pid1'])->find();
		if($tjname){
			$this->assign("tjname",$tjname['mobile']);
		}
		
		//红包、奖励金
		$where['user_id'] = $info_user['id'];
		if($info_user['is_pt']==1){
			$jlsum = db('hb_list')->where($where)->where('type','in','2,3')->sum('balance');
			$yjsum = db('hb_list')->where($where)->where('type','4')->sum('balance');
		}else{
			$jlsum = db('hb_list')->where($where)->where('type','1')->sum('balance');
			$yjsum = db('hb_list')->where($where)->where('type','5')->sum('balance');			
		}
		
		//获取当前用户订单
		$order_list = db('good_order')->where('user_id',session('user_id'))->select();
		
		$dfk_num = 0;
		$dfh_num = 0;
		$dsh_num = 0;
		$dpj_num = 0;
		foreach($order_list as $key=>$value){
			if($value['status'] == 1){
				$dfk_num = $dfk_num+1;
			}
			
			if($value['status'] == 2){
				$dfh_num = $dfh_num+1;
			}
			
			if($value['status'] == 3){
				$dsh_num = $dsh_num+1;
			}
			
			/*if($value['status'] == 4){
				$dpj_num = $dpj_num+1;
			}*/
		}
		$order_num['dfk_num'] = $dfk_num;
		$order_num['dfh_num'] = $dfh_num;
		$order_num['dsh_num'] = $dsh_num;
		//$order_num['dpj_num'] = $dpj_num;
		
		$this->assign("order_num",$order_num);
		$this->assign("jlsum",$jlsum);
		$this->assign("yjsum",$yjsum);
		$this->assign("url",'index');
       	return  $this->fetch();
    }
	
	//会员管理
	public function member(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$member = db('member')->where("city_id",$info_user['id'])->order('add_time desc')->limit($begin,10)->select();
			
			if($member){
				return json(['code'=>200,"list"=>$member]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//合伙人列表
    public function pt_list()
    {    	
		if($_POST){
			$info_user = session('user_info');
			$tjcount = db('member')->field('pt_id,count(id) as count')->group('pt_id')->buildSql();
			
			$page = 1;
			if(input("page")){
				$page = input("page");
			}
			$begin = ($page-1)*10;
			$ptlist = db('member')->alias('a')
			->join($tjcount." b","a.id=b.pt_id",'LEFT')
			->field('a.*,b.count')
			->where('a.is_pt',1)
			->where('a.city_id',$info_user['id'])->order('a.add_time desc')->limit($begin,10)->select();
			if($ptlist){
				return json(['code'=>200,"list"=>$ptlist]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
    }
	
	//合伙人申请列表
    public function ptsh_list()
    {    	
		if($_POST){
			$w['city_id']=session("user_info")['id'];
			$w['status']=1;
			$ptlist = db('pt_sq')->where($w)->order('add_time desc')->page(input('page'),10)->select();
			if($ptlist){
				return json(['code'=>200,"list"=>$ptlist]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
    }
	
	//红包记录
	public function hb_list(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$where['user_id'] = $info_user['id'];
			$where['type'] = '1';
			$logs=db('hb_list')->where($where)->order('add_time desc')->limit($begin,10)->select();
			if($logs){
				return json(['code'=>200,"list"=>$logs]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//佣金记录
	public function yj_list(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$where['user_id'] = $info_user['id'];
			$where['type'] = '4';
			$logs=db('hb_list')->where($where)->order('add_time desc')->limit($begin,10)->select();
			if($logs){
				return json(['code'=>200,"list"=>$logs]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//运营中心
	public function yunying(){
		$userinfo = db('member')->where('id',$info_user['id'])->find();
		$this->assign('is_city',$userinfo['is_city']);
		return  $this->fetch();
	}
	
	
	//奖励金发放
	function jljff(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		
		//累计奖励金发放人数
		$ljffrs = db('member')->where('is_rz',1)->where('city_id',$user_id)->count(); 		
		
		//待发放奖励金
		$config = get_config('ticket_config');
		$pthy = $config['m_total']; //普通会员奖励金
		$qyhy = $config['qy_total']; //普通会员奖励金
		
		$pthyrs = db('member')->where('city_id',$user_id)->where('is_rz',1)->where('is_qy',0)->count();  //普通会员人数
		$qyhyrs = db('member')->where('city_id',$user_id)->where('is_qy',1)->count();  //企业会员人数
		
		$pthyjlj = $pthyrs*$pthy;  //累计普通会员奖励金
		$qyhyjlj = $qyhyrs*$qyhy;  //累计企业会员奖励金
		
		//累计发放奖励金
		$ljffjlj = $pthyjlj + $qyhyjlj;		
		
		//累积已发放的奖励金
		$yffjlj = db('member')->where('city_id',$user_id)->sum('total_jlj');  
		
		//待发放奖励金
		$dffjlj = $ljffjlj - $yffjlj;
		
		//今日发放人数
		$jljrsd = db('hb_list')->where('city_id',$user_id)->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jljrsd',$jljrsd);
		
		//今日奖励金发放金额
		$jljzed = db('hb_list')->where('city_id',$user_id)->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jljzed',$jljzed);
		
		//查询列表
		$w['a.city_id'] = $user_id;
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('hb_list')->alias('a')
			->join("member b","a.user_id=b.id",'LEFT')
			->field('a.id,a.user_id,a.balance,a.add_time,b.nick_name')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}		
				
		$this->assign('ljffjlj',$ljffjlj);
		$this->assign('ljffrs',$ljffrs);
		$this->assign('yffjlj',$yffjlj);
		$this->assign('dffjlj',$dffjlj);
		return $this->fetch();
	}

	//直推佣金发放
	function ztyjff(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];

		//查询列表
		$w['a.city_id'] = $user_id;
		$w['a.type'] = 6;
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('gdyj_record')->alias('a')
			->join("member b","a.user_id=b.id",'LEFT')
			->field('a.id,a.user_id,a.balance,a.type,a.add_time,b.nick_name')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		$where['type'] = 6;
		$where['city_id'] = $user_id;
		//奖励金发放总额
		$jljze = db('gdyj_record')->where($where)->sum('balance');
		$this->assign('jljze',$jljze);
		
		//奖励金发放人数
		$jljrs = db('gdyj_record')->where($where)->group('user_id')->count();
		$this->assign('jljrs',$jljrs);
		
		//订单待发放佣金
		$ws['status'] = ['in',[2,3]];
		$ws['ct_id'] = $user_id;
		$goods_yj = db('good_order')->field('sum(tj_yj) as y1,sum(tj_yj2) as y2,sum(pt_yj) as y3,sum(ct_yj) as y4')->where($ws)->select();
		$dffsum = 0;
		foreach ($goods_yj as $key => $value) {
			$dffsum = $value['y1']+$value['y2']+$value['y3']+$value['y4'];
		}
		$this->assign('dffsum',$dffsum);
				
		//今日发放人数
		$jljrsd = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jljrsd',$jljrsd);
		
		//今日奖励金发放金额
		$jljzed = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jljzed',$jljzed);

		return $this->fetch();
	}

	//间推佣金发放
	function jtyjff(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];

		//查询列表
		$w['a.city_id'] = $user_id;
		$w['a.type'] = 7;
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('gdyj_record')->alias('a')
			->join("member b","a.user_id=b.id",'LEFT')
			->field('a.id,a.user_id,a.balance,a.type,a.add_time,b.nick_name')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		$where['type'] = 7;
		$where['city_id'] = $user_id;
		//奖励金发放总额
		$jljze = db('gdyj_record')->where($where)->sum('balance');
		$this->assign('jljze',$jljze);
		
		//奖励金发放人数
		$jljrs = db('gdyj_record')->where($where)->group('user_id')->count();
		$this->assign('jljrs',$jljrs);
		
		//订单待发放佣金
		$ws['status'] = ['in',[2,3]];
		$ws['ct_id'] = $user_id;
		$goods_yj = db('good_order')->field('sum(tj_yj) as y1,sum(tj_yj2) as y2,sum(pt_yj) as y3,sum(ct_yj) as y4')->where($ws)->select();
		$dffsum = 0;
		foreach ($goods_yj as $key => $value) {
			$dffsum = $value['y1']+$value['y2']+$value['y3']+$value['y4'];
		}
		$this->assign('dffsum',$dffsum);
				
		//今日发放人数
		$jljrsd = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jljrsd',$jljrsd);
		
		//今日奖励金发放金额
		$jljzed = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jljzed',$jljzed);

		return $this->fetch();
	}

	//平台合伙人佣金
	function ptyj(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];

		//查询列表	
		$w['a.type'] = 8;
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('gdyj_record')->alias('a')
			->join("member b","a.user_id=b.id",'LEFT')
			->field('a.id,a.user_id,a.balance,a.type,a.add_time,b.nick_name')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		$where['type'] = 8;
		//奖励金发放总额
		$jljze = db('gdyj_record')->where($where)->sum('balance');
		$this->assign('jljze',$jljze);
		
		//奖励金发放人数
		$jljrs = db('gdyj_record')->where($where)->group('user_id')->count();
		$this->assign('jljrs',$jljrs);
		
		//订单待发放佣金
		$ws['status'] = ['in',[2,3]];
		$ws['ct_id'] = $user_id;
		$goods_yj = db('good_order')->field('sum(tj_yj) as y1,sum(tj_yj2) as y2,sum(pt_yj) as y3,sum(ct_yj) as y4')->where($ws)->select();
		$dffsum = 0;
		foreach ($goods_yj as $key => $value) {
			$dffsum = $value['y1']+$value['y2']+$value['y3']+$value['y4'];
		}
		$this->assign('dffsum',$dffsum);
				
		//今日发放人数
		$jljrsd = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jljrsd',$jljrsd);
		
		//今日奖励金发放金额
		$jljzed = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jljzed',$jljzed);

		return $this->fetch();
	}

	//推荐合伙人奖励
	function tjptjl(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];

		//查询列表	
		$w['a.city_id'] = $user_id;
		$w['a.type'] = 9;
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('gdyj_record')->alias('a')
			->join("member b","a.user_id=b.id",'LEFT')
			->join("member c","a.pt_id=c.id",'LEFT')
			->field('a.id,a.user_id,a.balance,a.type,a.add_time,b.nick_name as name1,b.total_yj,c.nick_name as name2')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		$where['type'] = 9;
		$where['city_id'] = $user_id;
		//奖励金发放总额
		$jljze = db('gdyj_record')->where($where)->sum('balance');
		$this->assign('jljze',$jljze);
		
		//奖励金发放人数
		$jljrs = db('gdyj_record')->where($where)->group('user_id')->count();
		$this->assign('jljrs',$jljrs);
		
		//订单待发放佣金
		$ws['status'] = ['in',[2,3]];
		$ws['ct_id'] = $user_id;
		$goods_yj = db('good_order')->field('sum(tj_yj) as y1,sum(tj_yj2) as y2,sum(pt_yj) as y3,sum(ct_yj) as y4')->where($ws)->select();
		$dffsum = 0;
		foreach ($goods_yj as $key => $value) {
			$dffsum = $value['y1']+$value['y2']+$value['y3']+$value['y4'];
		}
		$this->assign('dffsum',$dffsum);
				
		//今日发放人数
		$jljrsd = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jljrsd',$jljrsd);
		
		//今日奖励金发放金额
		$jljzed = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jljzed',$jljzed);

		return $this->fetch();
	}
//提现审核通过
	function shenghe(){
		if($_POST){
			$status=input('status');
			$id=input("id");
			$tixian=db('member_tixian')->where("id",$id)->field("user_id,money")->find();
			if(!$tixian){
				return json(["code"=>400,'msg'=>'操作失败']);
			}
			$tx_id=$tixian['user_id'];
			$balance=$tixian['money'];
			//审核通过
			if($status==2){
				$sum=db('member_tixian')->wheretime('create_time','today')->where("status",2)->where("user_id",$tx_id)->sum('money');
				if($sum&&$sum>=20000){
					return json(["code"=>200,"msg"=>'每日提现不能超过2w申请失败']);
				}
				Db::startTrans();
				$result=true;
				$res=db('member_tixian')->where("id",$id)->update(['status'=>$status]);
				if(!$res){
					$result=false;
				}
				$sql="update cz_member set ytx_yj= ytx_yj + ".$balance." where id=".$tx_id ;
				$res=Db::execute($sql);
				if(!$res){
					$result=false;
				}
				$tx_user=db("member")->where('id',$tx_id)->field("open_id")->find();
				$payment=new PaymentController();
				$outTradeNo = order_create_no();
				$res=$payment->send_pay($tx_user['open_id'],$outTradeNo,$balance,'申请提现到账');
				if(!$res){
					$result=false;
				}
				if($result){
					Db::commit();
					return json(["code"=>200,"msg"=>'操作成功']);
				}else{
					Db::rollback();
					return json(["code"=>200,"msg"=>'操作失败']);
				}
			//审核不通过 
			}else{
				$res=db('member_tixian')->where("id",$id)->update(['status'=>$status]);
				return json(["code"=>200,"msg"=>'操作成功']);
			}
		}
	}
	//佣金提现管理
	function yjtxgl(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		$w['a.status']=1;
		//提现管理查询列表	
		$w['a.city_id'] = $user_id;
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('member_tixian')->alias('a')
			->join("member b","a.user_id=b.id",'LEFT')
			->field('a.id,a.user_id,a.money,a.status,a.create_time,b.nick_name,b.mobile')->where($w)->order("a.create_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		//累计提现总额
		$where['city_id'] = $user_id;
		$ljtxze = db('member_tixian')->where($where)->sum('money');
		$this->assign('ljtxze',$ljtxze);
		
		//累计提现人数
		$ljtxrs = db('member_tixian')->where($where)->group('user_id')->count();
		$this->assign('ljtxrs',$ljtxrs);
		
		//已提现佣金
		$ytxyj = db('member')->where($where)->sum('ytx_yj');
		$this->assign('ytxyj',$ytxyj);
				
		//待提现佣金
		$dtxyj = db('member')->where($where)->sum('yj');
		$this->assign('dtxyj',$dtxyj);
		
		//今日提现人数
		$jrtxrs = db('member_tixian')->where($where)->whereTime('create_time', 'today')->group('user_id')->count();
		$this->assign('jrtxrs',$jrtxrs);
		
		//今日提现金额
		$jrtxje = db('member_tixian')->where($where)->whereTime('create_time', 'today')->sum('money');
		$this->assign('jrtxje',$jrtxje);			
		return $this->fetch();
	}

	//收付款管理
	function skfk(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
			
		//提现管理查询列表	
		$w['a.city_id'] = $user_id;
		$w['a.order_type'] = ['in','1,2,5'];
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('paylog')->alias('a')
			->join("member b","a.user_id=b.id",'LEFT')
			->field('a.id,a.user_id,a.balance,a.order_type,a.state,a.add_time,b.nick_name')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		$where['city_id'] = $user_id;
		$where['state'] = 2;
		$where['order_type'] = ['in','1,2,5'];
		//累计收款总额
		$ljskze = db('paylog')->where($where)->sum('balance');
		$this->assign('ljskze',$ljskze);

		//今日收款总额
		$jrskje = db('paylog')->where($where)->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jrskje',$jrskje);	

		//今日收款人数
		$jrskrs = db('paylog')->where($where)->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jrskrs',$jrskrs);
		
		//累计收款人数
		$ljskrs = db('paylog')->where($where)->group('user_id')->count();
		$this->assign('ljskrs',$ljskrs);		
		return $this->fetch();
	}

	//运营中心合伙人佣金
	function yyptyj(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];

		//查询列表	
		$w['a.city_id'] = $user_id;
		$w['a.type'] = 8;
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('gdyj_record')->alias('a')
			->join("member b","a.user_id=b.id",'LEFT')
			->field('a.id,a.user_id,a.balance,a.type,a.add_time,b.nick_name')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		$where['type'] = 8;
		$where['city_id'] = $user_id;
		//奖励金发放总额
		$jljze = db('gdyj_record')->where($where)->sum('balance');
		$this->assign('jljze',$jljze);
		
		//奖励金发放人数
		$jljrs = db('gdyj_record')->where($where)->group('user_id')->count();
		$this->assign('jljrs',$jljrs);
		
		//订单待发放佣金
		$ws['status'] = ['in',[2,3]];
		$ws['ct_id'] = $user_id;
		$goods_yj = db('good_order')->field('sum(tj_yj) as y1,sum(tj_yj2) as y2,sum(pt_yj) as y3,sum(ct_yj) as y4')->where($ws)->select();
		$dffsum = 0;
		foreach ($goods_yj as $key => $value) {
			$dffsum = $value['y1']+$value['y2']+$value['y3']+$value['y4'];
		}
		$this->assign('dffsum',$dffsum);
				
		//今日发放人数
		$jljrsd = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jljrsd',$jljrsd);
		
		//今日奖励金发放金额
		$jljzed = db('gdyj_record')->where($where)->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jljzed',$jljzed);

		return $this->fetch();
	}

	//会员业绩与佣金
	function hyyj(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];

		//查询列表	
		$w['a.city_id'] = $user_id;
		$w['a.is_rz'] = 1;
		$w['a.is_pt'] = ['neq',1];
		if(input('name')){
			$w['a.id|a.mobile|a.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('member')->alias('a')
			->join("member b","a.pid1=b.id",'LEFT')
			->field('a.id,a.nick_name,a.mobile,a.is_qy,a.add_time,b.nick_name as tjname')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		
		$mem_id = input('id');
		if($mem_id){
			//自我消费业绩
			$w1['user_id'] = $mem_id;
			$w1['status'] = 4;
			$zwxf = db('good_order')->where($w1)->sum('pay_price');
			$this->assign('zwxf',$zwxf);
			
			//会所租用
			$w2['user_id'] = $mem_id;
			$w2['state'] = 1;
			$hszy = db('reserve')->where($w2)->sum('balance');
			$this->assign('hszy',$hszy);
			
			
			//直接推荐会员人数
			$zjtjrs = db('member')->where('pid1',$mem_id)->count();
			$this->assign('zjtjrs',$zjtjrs);
			
			//直接推荐会员业绩
			$zjtjyj = db('member')->where('pid1',$mem_id)->sum('total_xf');
			$this->assign('zjtjyj',$zjtjyj);
			
			//间接推荐会员人数
			$jjtjrs = db('member')->where('pid2',$mem_id)->count();
			$this->assign('jjtjrs',$jjtjrs);
			
			//间接推荐会员业绩
			$jjtjyj = db('member')->where('pid2',$mem_id)->sum('total_xf');
			$this->assign('jjtjyj',$jjtjyj);
			
			//推荐合伙人人数
			$tjptrs = db('member')->where('pid1',$mem_id)->where('is_pt',1)->count();
			$this->assign('tjptrs',$tjptrs);
			
			//推荐合伙人业绩
			$tjptyj = db('member')->where('pid1',$mem_id)->where('is_pt',1)->sum('total_xf');
			$this->assign('tjptyj',$tjptyj);
			
			//推荐运营中心人数
			$tjyyrs = db('member')->where('pid1',$mem_id)->where('is_city',1)->count();
			$this->assign('tjyyrs',$tjyyrs);
			
			//推荐运营中心业绩
			$tjyyyj = db('member')->where('pid1',$mem_id)->where('is_city',1)->sum('total_xf');
			$this->assign('tjyyyj',$tjyyyj);
			
			//会员累计奖励金
			$total_jlj = db('member')->field('total_jlj')->where('id',$mem_id)->find();
			$this->assign('total_jlj',$total_jlj['total_jlj']);
			
			//奖励天数
			$jlts = db('gdyj_record')->where('user_id',$mem_id)->count();
			$this->assign('jlts',$jlts);
			
			//一级佣金
			$yjyj = db('gdyj_record')->where('user_id',$mem_id)->where('type',6)->sum('balance');
			$this->assign('yjyj',$yjyj);
			
			//二级佣金
			$ejyj = db('gdyj_record')->where('user_id',$mem_id)->where('type',7)->sum('balance');
			$this->assign('ejyj',$ejyj);
			
			//合伙人订单角色佣金
			$ptjsyj = db('good_order')->where('pt_id',$mem_id)->sum('pay_price');
			$this->assign('ptjsyj',$ptjsyj);
		}else{
			$this->assign('zwxf',0);
			$this->assign('hszy',0);
			$this->assign('zjtjrs',0);
			$this->assign('zjtjyj',0);
			$this->assign('jjtjrs',0);
			$this->assign('jjtjyj',0);
			$this->assign('tjptrs',0);
			$this->assign('tjptyj',0);
			$this->assign('total_jlj',0);
			$this->assign('jlts',0);
			$this->assign('yjyj',0);
			$this->assign('ejyj',0);
			$this->assign('ptjsyj',0);
		}
		return $this->fetch();
	}

	//会员设置
	public function setpt(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		$w['a.city_id'] = $user_id;
		$w['a.is_pt'] = 1;
		if($_POST){
			if(input('name')){
			$w['a.id|a.mobile|a.real_name'] = ['like','%'.input('name').'%'];
			}
			$list = db('member')->alias('a')
			->join("member b","a.pid1=b.id",'LEFT')
			->field('a.id,a.real_name,a.mobile,a.add_time,b.nick_name as tjname')
			->where($w)->order("a.add_time desc")->page(1,3)->select();
			return json(['list'=>$list]);
		}	
		return $this->fetch();
	}
	
	//合伙人业绩与佣金
	function ptyjyj(){
		$where['is_pt']=1;
		$where['is_city']=2;
		$field="a.nick_name,a.id";
		$b=db("member")->where("pt_id=a.id")->field("count(id)")->buildSql();
		$c=db("gdyj_record")->where("user_id=a.id")->field("sum(balance)")->buildSql();
		
		$field.=",($b) as b";
		$field.=",($c) as c ";

		$list=db("member")->alias("a")
		->where($where)->field($field)->order("b desc")->limit(50)->select();
		$this->assign("list",$list);
		return $this->fetch();
	}

	//合伙人管理与查询
	public function pt(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		
		$w['a.city_id'] = $user_id;
		$w['a.is_pt'] = 1;
		if($_POST){
			if(input('name')){
				$w['a.id|a.mobile|a.real_name'] = ['like','%'.input('name').'%'];
			}
			$list = db('member')->alias('a')
			->join("member b","a.pid1=b.id",'LEFT')
			->field('a.id,a.nick_name,a.mobile,a.add_time,b.nick_name as tjname')->where($w)
			->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}	
		
		$w1['city_id'] = $user_id;
		$w1['is_pt'] = 1;		
		$data['count'] = db('member')->where($w1)->count();
		$data['month'] = db('member')->where($w1)->wheretime("add_time","month")->count();
		$data['today'] = db('member')->where($w1)->wheretime("add_time","today")->count();
		$this->assign('data',$data);
		return $this->fetch();
	}

	//会员角色佣金
	function memyj(){
		$user_info = session("user_info");
		$w['user_id'] = $user_info['id'];
		$config = get_config('ticket_config');
		$w['type'] = ['in','6,7,10,11,14,15'];
		if($_POST){
			$list = db('gdyj_record')->field('id,message,type,balance,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field = "count(id) as c,sum(balance) s";
		$log = db('gdyj_record')->where($w)->field($field)->find();
		if(!$log){
			$log['c'] = 0;
			$log['s'] = 0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}
	
	//合伙人角色佣金
	function hhryj(){
		$user_info = session("user_info");
		$w['user_id'] = $user_info['id'];
		$config = get_config('ticket_config');
		$w['type'] = ['in','8,9,12,16'];
		if($_POST){
			$list = db('gdyj_record')->field('id,message,type,balance,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field = "count(id) as c,sum(balance) s";
		$log = db('gdyj_record')->where($w)->field($field)->find();
		if(!$log){
			$log['c'] = 0;
			$log['s'] = 0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}
}
